package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service  //交给spring管理起来
@Transactional  //事务
public class CheckItemServiceImpl implements CheckItemService {

    //注入dao
    @Autowired
    private CheckItemDao dao;

    //添加检查项
    @Override
    public int add(CheckItem checkItem) {
        return dao.add(checkItem);
    }

    //分页查询检查项
    @Override
    public PageResult<CheckItem> findPage(QueryPageBean queryPageBean) {

        //使用分页插件,设置查询第几页,每页查询多少条
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //调用dao去执行查询操作
        Page<CheckItem> page = dao.findPage(queryPageBean);

        //构建PageResult对象,并返回
        return  new PageResult<>(page.getTotal(), page.getResult());
    }

    //删除检查项
    @Override
    public int delete(int id) {
        //先查询该检查组是否有被检查组使用
        long byId = dao.findUserCountById(id);
        //判断
        if (byId>0){
            System.out.println("该检查项存在被检查组使用的情况,禁止删除:"+id);
            return 0;
        }
        return dao.delete(id);
    }

    @Override
    public int update(CheckItem checkItem) {
        return dao.update(checkItem);
    }

}
